<?php
namespace Orm\Transaction;

use Base\Exception\RmException;
class TransactionClass extends \Base\BaseOrmObject{
    protected   $id,
				$open_id = '', // 发起支付人openid
				$uid = 0, // 用户ID
				$status = 0, // 交易状态，1.新交易，2.成功，3.失败
				$reference = 0, // 关联交易信息，订单ID
				$amount = 0, // 交易金额
				$platform = '', // 第三方支付平台
				$platform_id = '', // 第三方平台支付交易流水ID
				$out_trade_no = '', // 交易流水号
				$payment = 0, // 支付方式默认1：1-微信支付 2-支付宝支付
				$is_del = 0, // 默认0  是否删除 1是 0否
				$insert_time = '', // 插入时间
				$update_time = ''; // 更新时间

    /*
     *常量
     */
    const STATUS_NEW = 1; //交易状态，1.新交易
    const STATUS_SUCCEED = 2; //交易状态，2成功
    const STATUS_FAILURE = 3; //交易状态，3.失败

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\Transaction\TransactionModel();
        $data = $model->loadById($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'open_id' => $this->open_id,
			'uid' => $this->uid,
			'status' => $this->status,
			'reference' => $this->reference,
			'amount' => $this->amount,
			'platform' => $this->platform,
			'platform_id' => $this->platform_id,
			'out_trade_no' => $this->out_trade_no,
			'payment' => $this->payment,
        );

        $model = new \Orm\Transaction\TransactionModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'open_id' => $this->open_id,
			'uid' => $this->uid,
			'status' => $this->status,
			'reference' => $this->reference,
			'amount' => $this->amount,
			'platform' => $this->platform,
			'platform_id' => $this->platform_id,
			'out_trade_no' => $this->out_trade_no,
			'payment' => $this->payment,
        );

        $model = new \Orm\Transaction\TransactionModel();
        $model->update($data, "`id` = {$this->id}");
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\Transaction\TransactionModel();
        $model->update($data, "`id` = {$id}");
    }

    private function checkNecessaryParam()
    {
    }
}